package com.iam.edu.ad.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.iam.common.core.result.R;
import com.iam.edu.ad.dto.PromotionAdDTO;
import com.iam.edu.ad.dto.PromotionSpaceDTO;
import com.iam.edu.ad.entity.PromotionAd;
import com.iam.edu.ad.entity.PromotionSpace;
import com.iam.edu.ad.feign.RemoteAdService;
import com.iam.edu.ad.service.IPromotionAdService;
import com.iam.edu.ad.service.IPromotionSpaceService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  广告前端控制器
 * </p>
 *
 * @author kele
 * @since 2022-10-25
 */
@RestController
@AllArgsConstructor
@Slf4j
public class AdController implements RemoteAdService {

    private IPromotionSpaceService promotionSpaceService;

    private IPromotionAdService promotionAdService;

    /**
     * 查询所有广告定位信息列表
     * @return
     */
    @GetMapping(GET_ALL_SPACE)
    @ResponseBody
    @Override
    public R<List<PromotionSpace>> getAllSpace() {
        return R.ok(promotionSpaceService.list());
    }

    /**
     * 通过SpaceKey列表获取广告位以及对应的广告信息
     * @return
     */
    @GetMapping(GET_ALL_ADS)
    @ResponseBody
    public R<List<PromotionSpaceDTO>> getAllPromotionAds(@RequestParam("spaceKeys") String[] spaceKeys) {

        List<PromotionSpaceDTO> promotionSpaceDTOList = new ArrayList<>();

        for (String spaceKey:spaceKeys){

            //根据spaceKey获取PromotionSpace
            PromotionSpace promotionSpace = promotionSpaceService.getBySpaceKey(spaceKey);
            if (promotionSpace == null){
                continue;
            }
            //根据PromotionSpaceId获取对应的PromotionAd
            List<PromotionAd> promotionAds = promotionAdService.getByPromotionSpaceId(promotionSpace.getId());


            PromotionSpaceDTO promotionSpaceDTO = new PromotionSpaceDTO();
            List<PromotionAdDTO> promotionAdDTOS = new ArrayList<>(promotionAds.size());

            //拷贝promoteSpace对象的属性到promoteSpaceDTO
            BeanUtil.copyProperties(promotionSpace,promotionSpaceDTO);

            for (PromotionAd promotionAd : promotionAds) {
                PromotionAdDTO promotionAdDTO = new PromotionAdDTO();
                BeanUtil.copyProperties(promotionAd,promotionAdDTO);
                promotionAdDTOS.add(promotionAdDTO);
            }

            promotionSpaceDTO.setAdDTOList(promotionAdDTOS);
            promotionSpaceDTOList.add(promotionSpaceDTO);
        }

        return R.ok(promotionSpaceDTOList);
    }

    /**
     * 新增或者修改广告信息
     */
    @PostMapping(value = SAVE_OR_UPDATE_AD)
    @ResponseBody
    public R<Boolean> saveOrUpdate(@RequestBody PromotionAdDTO promotionAdDTO){
        PromotionAd entity = new PromotionAd();
        BeanUtil.copyProperties(promotionAdDTO,entity);
        //保存或者更新
        try {
            if(entity.getId() != null){
                //修改广告信息
                promotionAdService.updateById(entity);
            }else {
                //新增广告
                promotionAdService.save(entity);
            }
        }catch (Exception e){
            log.error("入库失败！",e);
            return R.failed(Boolean.FALSE);
        }
        return R.ok(Boolean.TRUE);
    }

    /**
     * 新增或者修改广告信息
     */
    @PostMapping(value = SAVE_OR_UPDATE_AD_SPACE)
    public R<Boolean> saveOrUpdateSpace(@RequestBody PromotionSpaceDTO promotionSpaceDTO){
        PromotionSpace entity = new PromotionSpace();
        BeanUtil.copyProperties(promotionSpaceDTO,entity);
        //保存或者更新
        try {
            if(entity.getId() != null){
                //修改广告位信息
                promotionSpaceService.updateById(entity);
            }else {
                //新增广告位
                promotionSpaceService.save(entity);
            }
        }catch (Exception e){
            log.error("入库失败！",e);
            return R.failed(Boolean.FALSE);
        }
        return R.ok(Boolean.TRUE);
    }

    /**
     *  根据ID获取对应的广告
     */
    @RequestMapping(GET_BY_ID)
    public R<PromotionAdDTO> getAdById(@RequestParam("id")Integer id){
        PromotionAdDTO promotionAdDTO = new PromotionAdDTO();
        PromotionAd promotionAd = promotionAdService.getById(id);
        if (promotionAd == null){
            return R.ok();
        }else {
            BeanUtil.copyProperties(promotionAd,promotionAdDTO);
            return R.ok(promotionAdDTO);
        }
    }

}

